package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxEventPush;
import com.budwk.app.miniapp.services.WxeventpushService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/event/push")
@ApiDefinition(tag = "微信小程序-事件推送")
@Slf4j
public class WxeventpushController {
    @Inject
    private WxeventpushService wxEventPushService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询WxEventPush")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxEventPush.class
    )
    @ApiResponses(
            implementation = WxEventPush.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxEventPush bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        return Result.data(wxEventPushService.listPage(pageNo, pageSize, cnd));
    }

    /**
     * 导出事件推送记录列表
     */
 /*   @SaCheckPermission("weixin:eventPush:export")
    @Log(title = "事件推送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxEventPushBo bo, HttpServletResponse response) {
        List<WxEventPushVo> list = iWxEventPushService.queryList(bo);
        ExcelUtil.exportExcel(list, "事件推送记录", WxEventPushVo.class, response);
    }*/

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增WxEventPush")
    @ApiFormParams(
            implementation = WxEventPush.class
    )
    @ApiResponses
    @SLog(tag = "新增WxEventPush", value = "${args[0].${table.pkName}}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxEventPush wxEventPush, HttpServletRequest req) {
        wxEventPush.setCreatedBy(SecurityUtil.getUserId());
        wxEventPushService.insert(wxEventPush);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改WxEventPush")
    @ApiFormParams(
            implementation = WxEventPush.class
    )
    @ApiResponses
    @SLog(tag = "修改WxEventPush", value = "${args[0].${table.pkName}}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxEventPush wxEventPush, HttpServletRequest req) {
        wxEventPush.setUpdatedBy(SecurityUtil.getUserId());
        wxEventPushService.updateIgnoreNull(wxEventPush);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取WxEventPush")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxEventPush wxEventPush = wxEventPushService.fetch(id);
        if (wxEventPush == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxEventPush);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除WxEventPush")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除WxEventPush", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxEventPush wxEventPush = wxEventPushService.fetch(id);
        if (wxEventPush == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxEventPushService.delete(id);
        req.setAttribute("_slog_msg", wxEventPush.getId());
        return Result.success();
    }
}
